#include "body.h"

Body::Body( void* owner, boost::shared_ptr<btDynamicsWorld> ownerWorld ) : m_ownerWorld(ownerWorld), m_owner(owner), m_totalWeight(0.0f)
{
}

/*BodypartSoftTorus* Body::addBodyPart_Soft_Torus(const btVector3& s, btSoftBodyWorldInfo& softbodyInfo, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr)
{
	totalWeight += weight;
	
	BodypartSoftTorus* b = new BodypartSoftTorus(
		s,
		softbodyInfo, 
		dynamic_cast<btSoftRigidDynamicsWorld*>(m_ownerWorld),
		m_owner,
		weight,
		localInertia,
		transform,
		clr
	);

	bodyparts.push_back( b );
	return b;
}*/

boost::shared_ptr<BodypartRigidBox> Body::addBodyPart_Rigid_Box(const btVector3& dimensions, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
{
	m_totalWeight += weight;
	
	boost::shared_ptr<BodypartRigidBox> b = boost::shared_ptr<BodypartRigidBox>(new BodypartRigidBox(
		dimensions,
		m_ownerWorld,
		m_owner,
		weight,
		localInertia,
		transform,
		clr,
		lin_damping, 
		ang_damping
	));

	m_bodyparts.push_back( b );
	return b;
}

/*BodypartRigidSphere* Body::addBodyPart_Rigid_Sphere(float radius, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
{
	totalWeight += weight;
	
	BodypartRigidSphere* b = new BodypartRigidSphere(
		radius,
		m_ownerWorld,
		m_owner,
		weight,
		localInertia,
		transform,
		clr,
		lin_damping, 
		ang_damping
	);

	bodyparts.push_back( b );
	return b;
}

BodypartRigidCylinder* Body::addBodyPart_Rigid_Cylinder(const btVector3& dimensions, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
{
	totalWeight += weight;
	
	BodypartRigidCylinder* b = new BodypartRigidCylinder(
		dimensions,
		m_ownerWorld,
		m_owner,
		weight,
		localInertia,
		transform,
		clr,
		lin_damping, 
		ang_damping
	);

	bodyparts.push_back( b );
	return b;
}*/

boost::shared_ptr<BodypartRigidTrianglemesh> Body::addBodyPart_Rigid_Trianglemesh(btVector3* gVertices, unsigned int numVertices, int* gVIndices, unsigned int numVIndices, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
{
	m_totalWeight += weight;
	
	boost::shared_ptr<BodypartRigidTrianglemesh> b = boost::shared_ptr<BodypartRigidTrianglemesh>(new BodypartRigidTrianglemesh(
		gVertices, numVertices, gVIndices, numVIndices, 
		m_ownerWorld,
		m_owner,
		weight,
		localInertia,
		transform,
		clr,
		lin_damping, 
		ang_damping
		));

	m_bodyparts.push_back( b );
	return b;
}

boost::shared_ptr<BodypartRigidTrianglemesh> Body::addBodyPart_Rigid_Trianglemesh(boost::shared_ptr<Model> model, const btVector3& scale, float weight, btVector3& localInertia, const btTransform& transform, const BeColor& clr, float lin_damping, float ang_damping)
{
	m_totalWeight += weight;
	
	boost::shared_ptr<BodypartRigidTrianglemesh> b = boost::shared_ptr<BodypartRigidTrianglemesh>(new BodypartRigidTrianglemesh(
		model,
		m_ownerWorld,
		m_owner,
		scale,
		weight,
		localInertia,
		transform,
		clr,
		lin_damping, 
		ang_damping
	));

	m_bodyparts.push_back( b );
	return b;
}

//void Body::addConstraint(unsigned int bodypartID1, unsigned int bodypartID2, const btTransform& localA, const btTransform& localB, double limitA, double limitB)
//{
/*	Constraint *c = new Constraint(
		m_ownerWorld,
		*bodyparts[bodypartID1]->body,
		*bodyparts[bodypartID2]->body,
		localA, localB,
		limitA, limitB
	);
	constraints.push_back( c );*/
//}

// void Body::addBodyPart_Capsule(void* owner, float width, float height, float weight, const btTransform& transform)
// {
// 	Bodypart *b = new Bodypart(m_ownerWorld);
// 	bodyparts.push_back( b );
// 
// 	b->shape = new btCapsuleShape(width, height);
// 
// 	btVector3 localInertia(0,0,0);
// 	if (weight != 0.f) // weight of non zero = dynamic
// 		b->shape->calculateLocalInertia(weight,localInertia);
// 
// 	btDefaultMotionState* myMotionState = new btDefaultMotionState(offset*transform);
// 	
// 	btRigidBody::btRigidBodyConstructionInfo rbInfo(weight,myMotionState,b->shape,localInertia);
// 	b->body = new btRigidBody(rbInfo);
// 
// 	m_ownerWorld->addRigidBody(b->body);
// 
//  	b->body->setUserPointer(owner);
// 	b->body->setDamping(0.05, 0.85);
// 	b->body->setDeactivationTime(0.001);
// 	b->body->setSleepingThresholds(1.6, 2.5);
// }

Body::~Body()
{
//		for ( unsigned int i=0; i < constraints.size(); i++ )
//			delete constraints[i];
}
